package com.zimi.controller;


import com.zimi.model.SysOrgExtend;
import com.zimi.service.SysDutyPrivilegeService;
import com.zimi.util.CommonUtil;
import com.zimi.util.GsonUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 职务功能授权表 api
 *
 * @author lnintr
 * @since 2019-05-22
 */
@RestController
@RequestMapping("sys-duty-privilege")
public class SysDutyPrivilegeApi {
    @Autowired
    private SysDutyPrivilegeService sysDutyPrivilegeService;

    @Autowired
    private CommonUtil commonService;

    @PostMapping("dutyFunctionList")
    @ApiOperation("查询机构和角色信息")
    public Object selectDutyFunctionList(SysOrgExtend record) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("List", sysDutyPrivilegeService.getFunctionlistByDuty(record));
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("dutyOrgList")
    @ApiOperation("查询机构和职务信息")
    public Object selectDutyOrgList(SysOrgExtend record) {
        Map<String, Object> objectMap = new HashMap<>();
        Map orgInfoMap = commonService.getOrgList();
        objectMap.put("List", sysDutyPrivilegeService.getOrglistByDuty(record));
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("saveFunctionDuty")
    @ApiOperation("修改权限和职务信息")
    public Object saveFunctionDuty(HttpServletRequest request) {
        String[] ma = request.getParameterValues("privilageList");

        List<SysOrgExtend> sysOrgExtends = GsonUtil.json2Objs(ma[0], SysOrgExtend.class);

        sysDutyPrivilegeService.saveFunctionDuty(sysOrgExtends, 1);

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("deleteFunctionDuty")
    @ApiOperation("删除权限和职务信息")
    public Object deleteFunctionDuty(HttpServletRequest request) {
        String[] ma = request.getParameterValues("privilageList");

        List<SysOrgExtend> sysOrgExtends = GsonUtil.json2Objs(ma[0], SysOrgExtend.class);

        sysDutyPrivilegeService.saveFunctionDuty(sysOrgExtends, 2);

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        return objectMap;
    }
}
